package com.web.cms.pan.provider;

import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.blue.validation.spi.IdListValidator;
import com.web.cms.pan.exception.ShareException;
import com.web.cms.pan.facade.ShareFacade;
import com.web.cms.pan.message.ShareLogRequest;
import com.web.cms.pan.message.ShareLogResponse;
import com.web.cms.pan.message.ShareRequest;
import com.web.cms.pan.message.ShareResponse;
import com.web.cms.pan.model.Share;
import com.web.cms.pan.model.ShareLog;
import com.web.cms.pan.service.ShareService;
import com.web.cms.pan.util.ShareConvertor;
import com.web.cms.pan.util.ShareLogConvertor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 共享接口实现类
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年04月09日
 */
@Component("panShareProvider")
public class ShareProvider implements ShareFacade
{
	@Autowired
	private ShareService shareService;
	
	public ShareProvider()
	{
	}

	@Override
	public void add(ShareRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		Share share = ShareConvertor.toShare(request);
		shareService.add(share);
	}

	@Override
	public void update(ShareRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		Share share = new Share();
		share.setId(request.getId());
		share.setUserId(request.getUserId());
		share.setName(request.getName());
		share.setPassword(request.getPassword());
		share.setState(request.getState());
		shareService.update(share);
	}

	@Override
	public ShareResponse get(Integer userId, Integer id)
	{
		if (userId == null)
			throw new ShareException("用户ID不能为空");
		if (id == null)
			throw new ShareException("ID不能为空");
			
		Share share = shareService.get(userId, id);
		ShareResponse response = ShareConvertor.toResponse(share);
		return response;
	}

	@Override
	public ShareResponse getByCode(Integer userId, String code)
	{
		if (userId == null)
			throw new ShareException("用户ID不能为空");
		if (code == null || code.isEmpty())
			throw new ShareException("代码不能为空");

		Share share = shareService.getByCode(userId, code);
		ShareResponse response = ShareConvertor.toResponse(share);
		return response;
	}

	@Override
	public ShareResponse valid(Integer userId, String code, String password)
	{
		if (userId == null)
			throw new ShareException("用户ID不能为空");
		if (code == null || code.isEmpty())
			throw new ShareException("代码不能为空");
		if (password == null || password.isEmpty())
			throw new ShareException("密码不能为空");

		Share share = shareService.valid(userId, code, password);
		ShareResponse response = ShareConvertor.toResponse(share);
		return response;
	}

	@Override
	public void delete(Integer userId, String ids)
	{
		if (userId == null)
			throw new ShareException("用户ID不能为空");
		if (ids == null || ids.isEmpty() || !IdListValidator.isValid(ids))
			throw new ShareException("ID格式错误");

		List<Integer> idList = Arrays.stream(ids.split(",")).map(Integer::valueOf).collect(Collectors.toList());
		shareService.delete(userId, idList);
	}

	@Override
	public Page<ShareResponse> listPage(ShareRequest request, int start, int size)
	{
		Share share = ShareConvertor.toShare(request);
		Page<Share> page = new Page<>(size);
		page.setCurPage(start);
		page = shareService.list(share, page);
		Page<ShareResponse> pageResponse = ShareConvertor.toResponse(page);
		return pageResponse;
	}

	@Override
	public Page<ShareLogResponse> listLogPage(ShareLogRequest request, int start, int size)
	{
		ShareLog shareLog = ShareLogConvertor.toShareLog(request);
		Page<ShareLog> page = new Page<>(size);
		page.setCurPage(start);
		page = shareService.listLog(shareLog, page);
		Page<ShareLogResponse> pageResponse = ShareLogConvertor.toResponse(page);
		return pageResponse;
	}

}
